//rc-shuffle-clust.c
//Programmer: Rory Carmichael
//Purpose: Shuffle a given cluster file and print the output

#include <stdio.h>
#include <unistd.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <math.h>
#include <time.h>

int **make_compressed_array(char *filename) {
	FILE *infile;
	char *result = NULL;
	char line [ 10000 ];
	char delims[] = "\t\n";
	int colcount = 0;
	int i = 0;
	infile = fopen(filename, "r");
	if(infile == NULL) {
		fprintf(stderr, "Failed to open %s\n", filename);
		return NULL;
	} else {
		if(fgets(line,sizeof(line),infile) != NULL) {
			result = strtok(line,delims);
			i = 0;
			while (result != NULL && (strcmp(result, "\n") != 0) && (strcmp(result,"") != 0)) {
				i++;
				result = strtok(NULL, delims);
			}
			colcount = i;
		}
	}
	rewind(infile);
	//Now we have the number of columns so we can allocate things nicely

	return infile;
}

FILE* open_and_count(char *filename, int *width, int *length) {
	FILE *infile;
	char *result = NULL;
	char line [ 10000 ];
	char delims[] = "\t\n";
	int colcount = 0;
	int linecount = 0;
	int i = 0;
	infile = fopen(filename, "r");
	if(infile == NULL) {
		fprintf(stderr, "Failed to open %s\n", filename);
	} else {
		while(fgets(line,sizeof(line),infile) != NULL) {
			result = strtok(line,delims);
			i = 0;
			while (result != NULL && (strcmp(result, "\n") != 0) && (strcmp(result,"") != 0)) {
				if (colcount != 0 && i > colcount) {
					fprintf(stderr, "MALFORMED INPUT FILE... %d (with line %s) is too many columns\n", i, line);
					exit(1);
				}
				i++;
				result = strtok(NULL, delims);
			}
			colcount = i;
			linecount++;
		}
	}
	*width = colcount;
	*length = linecount;
	rewind(infile);
	return infile;
}	

int main (int argc, char **argv) {
	int i = 0;
	int j = 0;
	int k = 0;
	int temp = 0;
	int width = 0;
	int length = 0;
	int index = 0;
	char *filename = NULL;
	FILE *infile;
	srand( time(NULL) );

	fprintf(stderr, "WE SAW %d ARGUMENTS\n", argc);
	if (argc != 2) {
		fprintf(stderr, "USAGE: rc-shuffle-clust <clusterfile>\n");
		exit(1);
	}
	filename = argv[1];
	infile = open_and_count(filename, &width, &length);
	fprintf(stderr, "Saw an input file with width %d and length %d\n", width, length);
	/*
	int **clusters = (int **) malloc(length*sizeof(int *));
	for(i=0;i<length;i++) {
		clusters[i] = (int *) malloc(width*sizeof(int));
	}
	*/
	int **new_clusters = (int **) malloc(length*sizeof(int *));
	for(i=0;i<length;i++) {
		new_clusters[i] = (int *) malloc(width*sizeof(int));
	}

	int *used = (int *) malloc(width*sizeof(int)):
	for(i=0;i<length;i++) {
		for(j=0;j<width;j++) {
			new_clusters[i][j] = 0;
		}
	}
	for(i=0;i<length;i++) {
		for(j=0;j<width;j++) {
			fprintf(stderr, "i: %d j: %d\n", i, j);
			fscanf(infile, "%d", &temp);
			//this is all stupid, let's compress it so we can shuffle properly
			if(temp != 0) {
				if ((length-temp-used[i]) < 0) {
					fprintf(stderr, "Uh Oh. Ran out of valid placement positions for value %d in row %d\n", temp, i);
					exit(1);
				}
				index = rand() % (length-temp-used[i])
				while (new_clusters[i][index] != 0) {
					index++;
				}
				new_clusters[i][index] = temp;
				used[i] += temp;
				k = index + 1;
				while((temp > 0) && (k < length)) {
					if(new_clusters[i][k] == 0) {
						new_clusters[i][k] = -1;
						k++; temp--;
					} else {
						k++;
					}
				}
			}
		}
	}

	for(i=0;i<length;i++) {
		for(j=0;j<width;j++) {
			fprintf(stderr, "%d\t", new_clusters[i][j]);
		}
		fprintf(stderr, "\n");
	}
	return 0;
}
